/**
 * 购物车调试工具
 * 用于诊断购物车数据结构问题
 */

// 模拟购物车API返回的数据结构
const mockCartResponse = {
    success: true,
    data: {
        list: [
            {
                id: 123,              // 购物车记录ID
                goods_id: 456,        // 商品ID
                sku_id: 789,          // SKU ID
                quantity: 2,           // 商品数量
                goods_name: "iPhone 15 Pro",
                sku_specs: "深空黑色@256GB",
                sku_price: 8999.00,
                goods_image: "https://example.com/iphone15pro.jpg"
            },
            {
                id: 124,
                goods_id: 457,
                sku_id: 790,
                quantity: 1,
                goods_name: "AirPods Pro",
                sku_specs: "白色@标准版",
                sku_price: 1999.00,
                goods_image: "https://example.com/airpodspro.jpg"
            }
        ]
    }
};

// 模拟form数据初始化
function initializeFormData(cartData) {
    return cartData.list.map(item => ({
        cart_id: item.id,        // 这里将item.id映射为cart_id
        goods_id: item.goods_id,
        sku_id: item.sku_id,
        quantity: item.quantity,
        selected: true,
        goods_name: item.goods_name,
        sku_specs: item.sku_specs,
        sku_price: item.sku_price,
        goods_image: item.goods_image
    }));
}

// 模拟数量更改
function simulateQuantityChange(cartData, formData, index, newQuantity) {
    console.log('=== 数量更改调试信息 ===');
    console.log('购物车原始数据:', cartData);
    console.log('表单数据:', formData);
    
    const cartItem = cartData.list[index];
    const formItem = formData[index];
    
    console.log('选中的购物车项目:', cartItem);
    console.log('选中的表单项目:', formItem);
    console.log('使用的cart_id:', formItem.cart_id);
    console.log('新的数量:', newQuantity);
    
    // 验证cart_id是否存在
    if (!formItem.cart_id) {
        console.error('❌ 错误: cart_id 为空!');
        console.error('formItem:', formItem);
        return false;
    }
    
    console.log('✅ cart_id 验证通过:', formItem.cart_id);
    
    // 模拟API调用
    const apiParams = {
        cart_id: formItem.cart_id,
        quantity: newQuantity
    };
    
    console.log('API调用参数:', apiParams);
    return true;
}

// 运行测试
console.log('=== 购物车数据结构测试 ===');

// 1. 初始化form数据
const formData = initializeFormData(mockCartResponse.data);
console.log('初始化后的form数据:', formData);

// 2. 测试数量更改
const testIndex = 0;
const newQuantity = 3;

const result = simulateQuantityChange(
    mockCartResponse.data, 
    formData, 
    testIndex, 
    newQuantity
);

if (result) {
    console.log('✅ 测试通过: 数量更改功能正常');
} else {
    console.log('❌ 测试失败: 发现问题');
}

// 3. 验证数据结构完整性
console.log('\n=== 数据结构验证 ===');
formData.forEach((item, index) => {
    console.log(`项目 ${index}:`);
    console.log(`  cart_id: ${item.cart_id} (${typeof item.cart_id})`);
    console.log(`  goods_id: ${item.goods_id} (${typeof item.goods_id})`);
    console.log(`  sku_id: ${item.sku_id} (${typeof item.sku_id})`);
    console.log(`  quantity: ${item.quantity} (${typeof item.quantity})`);
    console.log(`  selected: ${item.selected} (${typeof item.selected})`);
    console.log('  ---');
});

module.exports = {
    mockCartResponse,
    initializeFormData,
    simulateQuantityChange
};
